package com.linpq.road.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.core.domain.entity.SysDictData;
import com.linpq.common.core.service.CrudService;
import com.linpq.common.utils.DateUtils;
import com.linpq.common.utils.StringUtils;
import com.linpq.common.utils.uuid.IdUtils;
import com.linpq.road.domain.RdImportLog;
import com.linpq.road.domain.RdRoadSection;
import com.linpq.road.mapper.RdImportLogMapper;
import com.linpq.road.util.SerialNumberGenUtil;
import com.linpq.system.mapper.SysDictDataMapper;
import com.linpq.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linpq.road.mapper.RdRoadStakeMapper;
import com.linpq.road.domain.RdRoadStake;
import com.linpq.common.core.text.Convert;
import com.linpq.common.core.service.BaseService;
/**
 * 路桩Service业务层处理
 *
 * @author linpq
 * @date 2021-08-03
 */
@Service
public class RdRoadStakeService  extends CrudService<RdRoadStakeMapper, RdRoadStake> {

    @Autowired
    private RdRoadSectionService rdRoadSectionService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private RdRoadStakeMapper rdRoadStakeMapper;
    @Autowired
    SysDictDataMapper dictDataMapper;
    @Autowired
    RdImportLogMapper logMapper;

    private static final int FLAG=0;

    /**
     * 查询路桩
     *
     * @param id 路桩ID
     * @return 路桩
     */

    public RdRoadStake selectRdRoadStakeById(String id) {
        return dao.get(id);
    }

    /**
     * 查询路桩列表
     *
     * @param rdRoadStake 路桩
     * @return 路桩
     */

    public List<RdRoadStake> selectRdRoadStakeList(RdRoadStake rdRoadStake) {
        return dao.findList(rdRoadStake);
    }

    /**
     * 新增路桩
     *
     * @param rdRoadStake 路桩
     * @return 结果
     */

    public int insertRdRoadStake(RdRoadStake rdRoadStake) {
        rdRoadStake.setCreateTime(DateUtils.getNowDate());
        return super.save(rdRoadStake);
    }

    /**
     * 修改路桩
     *
     * @param rdRoadStake 路桩
     * @return 结果
     */

    public int updateRdRoadStake(RdRoadStake rdRoadStake) {
                rdRoadStake.setUpdateTime(DateUtils.getNowDate());
        return super.save(rdRoadStake);
    }

    /**
     * 删除路桩对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteRdRoadStakeByIds(String ids) {
        return dao.deleteRdRoadStakeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除路桩信息
     *
     * @param id 路桩ID
     * @return 结果
     */

    public int deleteRdRoadStakeById(String id) {
        return dao.delete(id);
    }

    /**
     * 对传入的路段编号批量插入路桩信息
     * @param id
     * @return
     */
    public AjaxResult insertByRoadStake(String id) throws SQLException {
        if (!StringUtils.isNull(id)&&!"".equals(id)){
            String[] split = id.split(",");
            for (int i = 0; i < split.length; i++) {
                //验证插入位，批量插入是否成功
                int flag=FLAG;
                RdRoadSection rdRoadSection = rdRoadSectionService.selectRdRoadSectionAllById(split[i]);
                //获取路桩间隔
                Integer stakeLength = Integer.valueOf(configService.selectConfigByKey("road.stake.length"));
                //管养单位序列号
                String serialRdManagementUnit= rdRoadSection.getRdManagementUnit().getSerialNumber();
                //高速编号
                String serialRdHighSpeedRoad = rdRoadSection.getRdHighSpeedRoad().getSerialNumber();
                //路段编号
                String serialNumber = rdRoadSection.getSerialNumber();
                //起始路桩
                String startPileNumber = rdRoadSection.getStartPileNumber();
                //终点路桩
                String endPileNumber = rdRoadSection.getEndPileNumber();
                //默认车道是否对称
                boolean laneSymmetry = Boolean.parseBoolean(configService.selectConfigByKey("road.lane.symmetry"));
                //上下车道
                Integer sectionLaneNumber = Integer.valueOf(configService.selectConfigByKey("road.section.lane.number"));

                List<Map<String, String>> roadStakeList =  SerialNumberGenUtil.generateSerialNumber(serialRdManagementUnit,serialRdHighSpeedRoad,
                        serialNumber,startPileNumber,endPileNumber,stakeLength,laneSymmetry,sectionLaneNumber,null);
                List<RdRoadStake> arrayList = new ArrayList();
                for (Map<String,String> m:roadStakeList) {
                    RdRoadStake rdRoadStake=new RdRoadStake();
                    rdRoadStake.preInsert();
                    rdRoadStake.setSerialNumber(m.get("roadStakeSerialNumber"));
                    //插入多个
                    rdRoadStake.setRoadSection(split[i]);
                    arrayList.add(rdRoadStake);
                }
                flag = rdRoadStakeMapper.insertRdRoadStake(arrayList);
                if (flag==0){
                    throw new SQLException();
                }
                //将数据存入到日志表
                int result = insertRoadImportLogByRoadSectionId(split[i]);
            }

        }
        return AjaxResult.success();
    }

    /**
     * 获取详细的路桩信息，包括路桩的基本信息，空间信息，高速信息，路段信息
     * 路面破损信息，路面结构信息，路面线性线性，交通信息，检测数据，路面性能信息
     * @return
     */
    public List<RdRoadStake> selectRdRoadStakeList() {
        return rdRoadStakeMapper.selectRdRoadStakeAllInfo();
    }

    /**
     * 批量插入日志信息
     * @return
     */
    public int insertRoadImportLogByRoadSectionId(String id) {
        //上行对象
        Integer roadDirectionUp = 0;
        Integer roadDirectionDown = 0;
        //导入结果
        Integer roadDirectionResultStateNot = 0;
        Integer roadDirectionResultStateAlready = 0;
        //车道路段编号
        Integer roadDirectionVehicleNumOne = 1;
        Integer roadDirectionVehicleNumTwo = 2;
        Integer roadDirectionVehicleNumThree = 3;
        Integer roadDirectionVehicleNumFour = 4;

        //上下行dictValue
        List<SysDictData> road_direction = dictDataMapper.selectDictDataByType("road_direction");
        for (SysDictData upOrDown : road_direction) {
            //字典验证上下行
            if (upOrDown.getDictLabel() == "上行") {
                roadDirectionUp = Integer.parseInt(upOrDown.getDictValue());
            } else {
                roadDirectionDown = Integer.parseInt(upOrDown.getDictValue());
            }
        }
        //导入结果dictValue
        List<SysDictData> sys_import_state = dictDataMapper.selectDictDataByType("sys_import_state");
        for (SysDictData resultState : sys_import_state) {
            //字典验证导入状态
            if (resultState.getDictLabel() == "未导入") {
                roadDirectionResultStateNot = Integer.parseInt(resultState.getDictValue());
            } else {
                roadDirectionResultStateAlready = Integer.parseInt(resultState.getDictValue());
            }
        }
        //车道编号
        List<SysDictData> vehicle_num = dictDataMapper.selectDictDataByType("vehicle_num");
        for (SysDictData vehicleNum : vehicle_num) {
            if ("1".equals(vehicleNum.getDictLabel()) ) {
                roadDirectionVehicleNumOne = Integer.parseInt(vehicleNum.getDictValue());
            }
            if ("2".equals(vehicleNum.getDictLabel())) {
                roadDirectionVehicleNumTwo = Integer.parseInt(vehicleNum.getDictValue());
            }
            if ("3".equals(vehicleNum.getDictLabel())) {
                roadDirectionVehicleNumThree = Integer.parseInt(vehicleNum.getDictValue());
            }
            if ("4".equals(vehicleNum.getDictLabel())) {
                roadDirectionVehicleNumFour = Integer.parseInt(vehicleNum.getDictValue());
            }
        }
        //获取导入：类型dictValue
        List<SysDictData> rd_data_type = dictDataMapper.selectDictDataByType("rd_data_type");
        List<RdImportLog> sysDictData = new ArrayList<>();
        for (SysDictData rdDateType : rd_data_type) {
            RdImportLog rdImportLogVehicleNumOne = new RdImportLog();
            rdImportLogVehicleNumOne.preInsert();
            //路段id
            rdImportLogVehicleNumOne.setRoadSectionId(id);
            //导入类型
            rdImportLogVehicleNumOne.setImportType(rdDateType.getDictValue());
            //导入结果
            rdImportLogVehicleNumOne.setResult(roadDirectionResultStateNot);
            //导入状态
            rdImportLogVehicleNumOne.setImportState(roadDirectionResultStateNot);
            //上下行
            rdImportLogVehicleNumOne.setUpOrDown(roadDirectionUp);
            //车道
            rdImportLogVehicleNumOne.setXlane(roadDirectionVehicleNumOne);

            RdImportLog rdImportLogVehicleNumTwo = new RdImportLog();
            rdImportLogVehicleNumTwo.preInsert();
            //路段id
            rdImportLogVehicleNumTwo.setRoadSectionId(id);
            //导入类型
            rdImportLogVehicleNumTwo.setImportType(rdDateType.getDictValue());
            //导入结果
            rdImportLogVehicleNumTwo.setResult(roadDirectionResultStateNot);
            //导入状态
            rdImportLogVehicleNumTwo.setImportState(roadDirectionResultStateNot);
            //上下行
            rdImportLogVehicleNumTwo.setUpOrDown(roadDirectionUp);
            //车道
            rdImportLogVehicleNumTwo.setXlane(roadDirectionVehicleNumTwo);

            RdImportLog rdImportLogVehicleNumThree = new RdImportLog();
            rdImportLogVehicleNumThree.preInsert();
            //路段id
            rdImportLogVehicleNumThree.setRoadSectionId(id);
            //导入类型
            rdImportLogVehicleNumThree.setImportType(rdDateType.getDictValue());
            //导入结果
            rdImportLogVehicleNumThree.setResult(roadDirectionResultStateNot);
            //导入状态
            rdImportLogVehicleNumThree.setImportState(roadDirectionResultStateNot);
            //上下行
            rdImportLogVehicleNumThree.setUpOrDown(roadDirectionDown);
            //车道
            rdImportLogVehicleNumThree.setXlane(roadDirectionVehicleNumThree);

            RdImportLog rdImportLogVehicleNumFour = new RdImportLog();
            rdImportLogVehicleNumFour.preInsert();
            //路段id
            rdImportLogVehicleNumFour.setRoadSectionId(id);
            //导入类型
            rdImportLogVehicleNumFour.setImportType(rdDateType.getDictValue());
            //导入结果
            rdImportLogVehicleNumFour.setResult(roadDirectionResultStateNot);
            //导入状态
            rdImportLogVehicleNumFour.setImportState(roadDirectionResultStateNot);
            //上下行
            rdImportLogVehicleNumFour.setUpOrDown(roadDirectionDown);
            //车道
            rdImportLogVehicleNumFour.setXlane(roadDirectionVehicleNumFour);

            sysDictData.add(rdImportLogVehicleNumOne);
            sysDictData.add(rdImportLogVehicleNumTwo);
            sysDictData.add(rdImportLogVehicleNumThree);
            sysDictData.add(rdImportLogVehicleNumFour);
        }
        int i = logMapper.insertRdRoadImportInfo(sysDictData);
        return i;
    }
}
